/**
 * Copyright (C), 2011-2016
 * File Name: QCommand.java
 * Encoding: UTF-8
 * Date: Feb 28, 2012
 * History: 
 */
package com.waynewan.qcomet.message;

import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * clientId|action(subscription/unsubscription a event)|eventName(the event name)|payload
 * @author WayneWan (bestirwiny@gmail.com)
 * @Date Feb 28, 2012 9:09:05 PM
 */
public class QCommand {
	public static final String ACTION_SUBSCRIPTION    = "subscription";
	public static final String ACTION_UNSUBSCRIPTIONE = "unsubscription";
	private static final String REGX = "^\\w+\\|(" + ACTION_SUBSCRIPTION + "|" + ACTION_UNSUBSCRIPTIONE + ")\\|\\w+\\|.*$";
	
	private String client;
	
	private String action;
	
	private String eventName;
	
	private String payload;

	public static QCommand parserCmd(String cmd){
		if(!Pattern.matches(REGX, cmd)) throw new RuntimeException("command can not recognize!");
		String[] cmds = StringUtils.split(cmd, "|");
		QCommand command = new QCommand();
		command.setClient(cmds[0]);
		command.setAction(cmds[1]);
		command.setEventName(cmds[2]);
		if(cmds.length > 3) command.setPayload(cmds[3]);
		
		return command;
	}
	
	public String getClient() {
		return client;
	}

	public void setClient(String client) {
		this.client = client;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getPayload() {
		return payload;
	}

	public void setPayload(String payload) {
		this.payload = payload;
	}
	
	public String getEventName() {
		return eventName;
	}

	public void setEventName(String eventName) {
		this.eventName = eventName;
	}

	public static void main(String[] arhs){
	}
}
